/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.suse.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.kaixin.connect.Kaixin;
import com.kaixin.connect.exception.KaixinAuthError;
import com.kaixin.connect.listener.KaixinAuthListener;
import com.kaixin.connect.loginwithoutwebview.LoginParameter;
import com.kaixin.connect.loginwithoutwebview.PopUpDialog;


public class StartActivity extends Activity {
	private static final String 		TAG = "AndroidExample";
	private	static final int			LOGINERROR	= 1001;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOGINERROR:
				Toast.makeText(StartActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};
	
	KaixinAuthListener authListener = new KaixinAuthListener() {
		@Override
		public void onAuthCancel(Bundle values) {
			Log.i(TAG, "onAuthCancel");
		}

		@Override
		public void onAuthCancelLogin() {
			Log.i(TAG, "onAuthCancelLogin");
		}

		@Override
		public void onAuthComplete(Bundle values) {
//			Kaixin kaixin = Kaixin.getInstance();
//			try {
//				String response = kaixin.refreshAccessToken(AndroidExample.this, null);
//			} catch (FileNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (MalformedURLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			Intent intent = new Intent(StartActivity.this, MainActivity.class);
			startActivity(intent);
		}

		@Override
		public void onAuthError(KaixinAuthError kaixinAuthError) {
			Log.i(TAG, "onAuthError");
			Message msg = Message.obtain();
			msg.obj = kaixinAuthError.getErrorDescription();
			msg.what = LOGINERROR;
			mHandler.sendMessage(msg);
			
		}
	};

	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		public void onClick(View v) {
			Kaixin kaixin = Kaixin.getInstance();
			kaixin.loadStorage(StartActivity.this);
			if (kaixin.isSessionValid()) {
				Intent intent = new Intent(StartActivity.this,
						MainActivity.class);
				startActivity(intent);
			} else if(v.getId() == R.id.oauth_layout_username_password){
				String[] permissions = {"basic", "create_records","friends_records","friends_birthday","friends_bodyform","friends_blood","friends_marriage","friends_intro","friends_education","friends_repaste"};
				LoginParameter param = new LoginParameter(null, null, permissions);
				PopUpDialog dialog = new PopUpDialog(StartActivity.this, param, authListener);
				dialog.show();
			} else if(v.getId() == R.id.oauth_layout){
				String[] permissions = {"basic", "create_records","friends_records","user_birthday","user_bodyform","user_blood","user_marriage","user_intro","user_education","user_online","friends_repaste"};
				kaixin.authorize(StartActivity.this, permissions, authListener);
			}
		}

	};
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.start);

		// ���е�¼��Ȩ
		View beginOuathBtn = (View) findViewById(R.id.oauth_layout);
		beginOuathBtn.setOnClickListener(mOnClickListener);
		View userOuathBtn = (View) findViewById(R.id.oauth_layout_username_password);
		userOuathBtn.setOnClickListener(mOnClickListener);
	}
}